//
//  SecondPageViewController.swift
//  DriverSchoolAPP
//
//  Created by Logel on 2017/5/10.
//  Copyright © 2017年 Logel. All rights reserved.
//

import UIKit

class SecondPageViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
    @IBOutlet weak var mySeg: UISegmentedControl!
    //var tbView = UITableView()
    var tableV = UITableView()
    var scrollVIew = UIScrollView()
    var stype = 0
    var selectNum = -1
    override func viewDidLoad() {
        super.viewDidLoad()
       
       
        setUpView()
        
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func doPageing(_ sender: Any) {
        let pageNum = CGFloat(mySeg.selectedSegmentIndex)
        switch mySeg.selectedSegmentIndex {
        case 0...3:
            scrollVIew.contentOffset = CGPoint(x: KMainScreenWidth * pageNum, y: 0)
        default:
            break
        }
    }
    

    func setUpView() {
        scrollVIew = UIScrollView.init(frame: CGRect(x: 0, y: 105, width: KMainScreenWidth, height:KMainScreenHeight - 160))
        //scrollVIew.backgroundColor = UIColor.red
        scrollVIew.isScrollEnabled = false
        scrollVIew.contentSize = CGSize(width: KMainScreenWidth * 4, height: KMainScreenHeight - 160)
        scrollVIew.isPagingEnabled = true
        
        self.view.addSubview(scrollVIew)
        
       
        setUpFirstPage()
        setUpSecondPage()
    }
    
    func setUpFirstPage(){
        let view1 = UIView.init(frame: CGRect(x: 0, y: 202, width: KMainScreenWidth, height: 17))
            view1.backgroundColor = KP_BG_GRAY
//        
//        let view2 = UIView.init(frame: CGRect(x: KMainScreenWidth/2, y: 0, width: 1, height: 150))
//        view2.backgroundColor = tableLineColor
//        
//        let view3 = UIView.init(frame: CGRect(x: KMainScreenWidth/2-1, y: 75, width: KMainScreenWidth/2-1, height: 1))
//        view3.backgroundColor = tableLineColor
//        
//        let view4 = UIView.init(frame: CGRect(x: KMainScreenWidth*3/4-1, y: 0, width: 1, height: 150))
//        view4.backgroundColor = tableLineColor
//        
//        let view5 = UIView.init(frame: CGRect(x: 0, y: 150, width: KMainScreenWidth, height: 1))
//        view5.backgroundColor = tableLineColor
        
        let btn1 = UIButton(frame: CGRect(x: KMainScreenWidth/3+1, y: 1, width: KMainScreenWidth/3-2, height: 200))
        btn1.setBackgroundImage(UIImage(named:"2-1-z"), for: .normal)
        btn1.tag = 2
        
        let btn2 = UIButton(frame: CGRect(x: KMainScreenWidth*2/3+1, y: 1, width: KMainScreenWidth/3-2, height: 100))
        btn2.setBackgroundImage(UIImage(named:"2-1-y-ys"), for: .normal)
        btn2.tag = 2
        
        let btn3 = UIButton(frame: CGRect(x: KMainScreenWidth*2/3+1, y: 101, width: KMainScreenWidth/3-2, height: 100))
        btn3.setBackgroundImage(UIImage(named:"2-1-y-yx"), for: .normal)
        btn3.tag = 5
        
        let btn4 = UIButton(frame: CGRect(x: 1, y: 1, width: KMainScreenWidth/3-2, height: 100))
        btn4.setBackgroundImage(UIImage(named:"2-1-y-zs"), for: .normal)
        btn4.tag = 1
        
        let btn5 = UIButton(frame: CGRect(x: 1, y: 101, width: KMainScreenWidth/3-2, height: 100))
        btn5.setBackgroundImage(UIImage(named:"2-1-y-zx"), for: .normal)
        btn5.tag = 6
        
        let btn6 = UIButton(frame: CGRect(x: 1, y: 220, width: KMainScreenWidth/3-2, height: 120))
        btn6.setBackgroundImage(UIImage(named:"2-1-x-zz"), for: .normal)
        btn6.tag = 4
        
        let btn7 = UIButton(frame: CGRect(x: KMainScreenWidth/3+1, y: 220, width: KMainScreenWidth/3-2, height: 120))
        btn7.setBackgroundImage(UIImage(named:"2-1-x-z"), for: .normal)
        btn7.tag = 7
        
        let btn8 = UIButton(frame: CGRect(x: KMainScreenWidth*2/3+1, y: 220, width: KMainScreenWidth/3-2, height: 120))
        btn8.setBackgroundImage(UIImage(named:"2-1-x-y"), for: .normal)
        btn8.tag = 8
        
        btn1.addTarget(self, action: #selector(doToGo(btn:)), for: .touchUpInside)
        btn2.addTarget(self, action: #selector(doToGo(btn:)), for: .touchUpInside)
        btn3.addTarget(self, action: #selector(doToGo(btn:)), for: .touchUpInside)
        btn4.addTarget(self, action: #selector(doToGo(btn:)), for: .touchUpInside)
        btn5.addTarget(self, action: #selector(doToGo(btn:)), for: .touchUpInside)
        btn6.addTarget(self, action: #selector(doToGo(btn:)), for: .touchUpInside)
        btn7.addTarget(self, action: #selector(doToGo(btn:)), for: .touchUpInside)
        btn8.addTarget(self, action: #selector(doToGo(btn:)), for: .touchUpInside)

        
        
        scrollVIew.addSubview(btn1)
        scrollVIew.addSubview(btn2)
        scrollVIew.addSubview(btn3)
        scrollVIew.addSubview(btn4)
        scrollVIew.addSubview(btn5)
        scrollVIew.addSubview(btn6)
        scrollVIew.addSubview(btn7)
        scrollVIew.addSubview(btn8)
        
        
        scrollVIew.addSubview(view1)
//        scrollVIew.addSubview(view2)
//        scrollVIew.addSubview(view3)
//        scrollVIew.addSubview(view4)
//        scrollVIew.addSubview(view5)
    }
    
    func setUpSecondPage(){
        tableV = UITableView(frame: CGRect(x: KMainScreenWidth, y: 0, width: KMainScreenWidth, height: KMainScreenHeight-160))
        //let player = ZYPlayer(nibName: "ZYPlayer", bundle: nil, onView: page2, orgFrame: CGRect(x: 5, y: 5, width: KMainScreenWidth/2-10, height:140) , url: "http://media.vtibet.com/masvod/public/2014/01/23/20140123_143bd4c1b14_r1_300k.mp4")
        // 下面是可选的属性设置
        //player.fillMode = .resizeAspectFill
        tableV.register(UINib(nibName:"KeMu2TableViewCell",bundle:nil), forCellReuseIdentifier: "reuseCell")
        tableV.delegate = self
        tableV.dataSource = self

        
        scrollVIew.addSubview(tableV)
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseCell", for: indexPath) as! KeMu2TableViewCell
        cell.selectNum = indexPath.row
       
        return cell
    }
    
     func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 105
    }
    
     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         selectNum = indexPath.row
         self.performSegue(withIdentifier: "toVideo", sender: nil)
    }
    
    func doToGo(btn:UIButton){
        if user.accountId != "" {
            stype = btn.tag
            switch btn.tag {
            case 5:
                self.performSegue(withIdentifier: "toKeyPoint", sender: nil)
            case 6:
                self.performSegue(withIdentifier: "toZXLX", sender: nil)
            case 7:
                self.performSegue(withIdentifier: "toRecord", sender: nil)
            case 8:
                self.performSegue(withIdentifier: "toMyError", sender: nil)
            default:
                self.performSegue(withIdentifier: "toQuestion", sender: nil)
            }
        }else{
            showMsg(msg: "请先登录！")
        }
        
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "toQuestion" {
            let dest = segue.destination as! QuestionViewController
            dest.qtype = stype
        }else if segue.identifier == "toVideo"{
            let dest = segue.destination as! VideoViewController
            switch selectNum {
            case 0:
                dest.vURL = V_DK_URL
            case 1:
                dest.vURL = V_CF_URL
            case 2:
                dest.vURL = V_ZJ_URL
            case 3:
                dest.vURL = V_SW_URL
            default:
                break
            }
        }
    }
}
